package customize

import (
	"errors"
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	"gorm.io/gorm"
)

type MedicalHistoryService struct {
}

// SaveMedicalHistory 保存用户病史
func (mh *MedicalHistoryService) SaveMedicalHistory(umh *customize.UserMedicalHistory) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		var history customize.UserMedicalHistory
		info := tx.Where("id = ?", umh.ID).First(&history)
		if errors.Is(info.Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
			info = tx.Create(&umh)
		} else {
			info = tx.Where("id = ?", umh.ID).Updates(umh)
		}
		return err
	})
	return
}

// GetUserMedicalHistory 获取用户病史
func (mh *MedicalHistoryService) GetUserMedicalHistory(id uint) (history customize.UserMedicalHistory, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&history).Error
	return
}
